﻿using Autofac;
using Autofac.Extras.Quartz;
using Solution.Application.Jobs;
using System;
using System.Collections.Specialized;

namespace Solution.Application.Injection
{
    public static class QuartzJobsInjection
    {
        public static void AddJobs(this ContainerBuilder builder)
        {
            builder.RegisterModule(new QuartzAutofacFactoryModule { ConfigurationProvider = QuartzConfigurationProvider });
            builder.RegisterModule(new QuartzAutofacJobsModule(typeof(IDependencyJob).Assembly));
        }

        private static NameValueCollection QuartzConfigurationProvider(IComponentContext arg)
        {
            return new NameValueCollection
            {
                ["quartz.scheduler.instanceName"] = "Solution.Application.Jobs",
                ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
                ["quartz.threadPool.threadCount"] = "5",
                ["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins",
                ["quartz.plugin.xml.fileNames"] = "Configs/quartz_jobs.xml",
                ["quartz.plugin.xml.FailOnFileNotFound"] = "true",
                ["quartz.plugin.xml.failOnSchedulingError"] = "true"
            };
        }
    }
}